package com.lagouedu.cfgcenter.config;

import com.lagouedu.cfgcenter.Constants;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DatabaseConfig {

    private String datasourceUrl;

    private String datasourceUserName;

    private String datasourcePassword;

    private String driverClass;



    // 初始化数据库配置
    @Bean("dataResourceConfig")
    public DatabaseConfig getDatabaseConfig(){

        String url = Constants.zkClient.readData(Constants.datasourceUrl);
        String dClass = Constants.zkClient.readData(Constants.driverClass);
        String userName = Constants.zkClient.readData(Constants.datasourceUserName);
        String password = Constants.zkClient.readData(Constants.datasourcePassword);

        DatabaseConfig databaseConfig = new DatabaseConfig();
        databaseConfig.setDatasourceUrl(url);
        databaseConfig.setDriverClass(dClass);
        databaseConfig.setDatasourceUserName(userName);
        databaseConfig.setDatasourcePassword(password);
        return databaseConfig;
    }


    public Map toMap(){
        Map map = new HashMap();
        map.put("driverClassName",getDriverClass());
        map.put("url",getDatasourceUrl());
        map.put("username",getDatasourceUserName());
        map.put("password",getDatasourcePassword());
        map.put("initialSize","5");
        map.put("maxActive","10");
        map.put("maxWait","3000");
        map.put("maxIdle","6");
        map.put("minIdle","3");
        return map;
    }

    public String getDatasourceUrl() {
        return datasourceUrl;
    }

    public void setDatasourceUrl(String datasourceUrl) {
        this.datasourceUrl = datasourceUrl;
    }

    public String getDatasourceUserName() {
        return datasourceUserName;
    }

    public void setDatasourceUserName(String datasourceUserName) {
        this.datasourceUserName = datasourceUserName;
    }

    public String getDatasourcePassword() {
        return datasourcePassword;
    }

    public void setDatasourcePassword(String datasourcePassword) {
        this.datasourcePassword = datasourcePassword;
    }

    public String getDriverClass() {
        return driverClass;
    }

    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

}
